package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import proxy.ProxyFactory;
import service.BoxAndPackageService;
import service.BoxAndPackageServiceInterface;
import service.TerminalService;
import service.TerminalServiceInterface;
import utils.GetHelper;

import beans.Agent;
import beans.BoxDetails;
import beans.Branch;
import beans.Config;
import beans.Terminal;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import dao.BoxDao;
import dao.BranchDao;
import dao.ConfigDao;
import dao.TerminalDao;

@SuppressWarnings("serial")
public class TerminalAction extends ActionSupport implements ModelDriven<Terminal>{

	private Terminal terminal = new Terminal();
	private Config config = null;
	private BoxDetails boxDetails = null;

	private String returnType;//返回类型
	private String pageName;//返回页面
	private Map<String, Object> terminalMap;
	
	private int rows;// easyui页大小
    private int page;// easyui当前页
    private String ids;//删除id集合

	private int terminalID;// 终端ID
	private int branchID;//设备网点ID
	private String oldpassword;// 旧密码
	private String newpassword;// 新密码
	
	private TerminalDao terminalDao = new TerminalDao();
	private BranchDao branchDao = new BranchDao();
	private BoxDao boxDao = new BoxDao();
	ConfigDao configDao = new ConfigDao();
	
	private List<Terminal> terminals;
	private List<Branch> branches;
	List<Config> configs = configDao.getAllconfig();
	
	ActionContext actionContext = ActionContext.getContext();
	Map<String, Object> session = actionContext.getSession();
	Agent agent = (Agent) session.get("admin");
	
	private BoxAndPackageServiceInterface boxPkgService = (BoxAndPackageServiceInterface) ProxyFactory
			.getProxyInstance(new BoxAndPackageService());
	
	private TerminalServiceInterface terminalService = (TerminalServiceInterface) ProxyFactory
	        .getProxyInstance(new TerminalService());
	
	public String returnJSP() {
        return pageName;
    }
	
	/**
	 * 
	 * 获取列表
	 * @author Devil Zheng
	 * @created 2015-9-15 下午3:20:30
	 * @return
	 */
	public String getTerminalPage() {
        terminals = terminalDao.returnAllTermials(agent);
        if (returnType.equals("json")) {
            terminalMap = new HashMap<String, Object>();
            terminalMap.put("total", terminalService.getTerminalNumber(agent));
            terminalMap.put("rows", terminalService.getTerminalPage(agent, rows, page));
            return "jsonSuccess";
        }else{
            return "terminalList";
        }
    }
	
	/**
	 * 
	 * 删除
	 * @author Devil Zheng
	 * @created 2015-9-15 下午3:33:53
	 * @return
	 */
	public String deleteTerminal(){
        terminalMap = new HashMap<String, Object>();
        try {
            terminalService.deleteTerminal(ids);
            terminalMap.put("success", true);
            terminalMap.put("msg", "删除成功");
        } catch (Exception e) {
            terminalMap.put("success", false);
            terminalMap.put("msg", "删除失败"+e.toString());
        }
        return "jsonSuccess";
    }
	
	/**
	 * 
	 * 添加设备
	 * @author Devil Zheng
	 * @created 2015-9-16 上午9:54:27
	 * @return
	 */
	public String addTerminal() {
        terminal.setBranch(branchDao.getBranchById(branchID));
        terminalDao.save(terminal);
        boxPkgService.updateBoxRemark(terminal);
        return "jsonSuccess";
    }

	/**
	 * 获取所有的网点
	 * 
	 * @return
	 */
	public String getAllBranch() {
		branches = branchDao.getAll(agent);
		return "success";
	}

	/**
	 * 获取终端信息
	 * 
	 * @return
	 */
	public String getTerminalById() {
		terminal = terminalDao.getTerminalById(terminalID);
		getAllBranch();
		return "success";
	}

	/**
	 * 编辑终端
	 * 
	 * @return
	 */
	public String editTerminal() {
		Terminal t = terminalDao.getTerminalById(terminalID);
		terminal.setBranch(branchDao.getBranchById(branchID));
		terminal.setTerminalId(terminalID);
		terminal.setHardVer(t.getHardVer());
		terminal.setSoftVer(t.getSoftVer());
		terminal.setStatus(t.getStatus());
		terminal.setLoginTime(t.getLoginTime());
		terminal.setBarcodeStatus(t.getBarcodeStatus());
		terminal.setCardReadStatus(t.getCardReadStatus());
		terminal.setTouchStatus(t.getTouchStatus());
		terminal.setNetworkIP(t.getNetworkIP());
		terminal.setNetworkType(t.getNetworkType());
		terminal.setSimcardNum(t.getSimcardNum());
		terminalDao.edit(terminal);
		config = configs.get(0);
		GetHelper
		.doHttpGet(config.getValue()+"controller?funcID=0030&terminalId="
				+ terminalID);
		String str = "controller?funcID=0029&terminalId="+ terminalID;
		GetHelper
				.doHttpGet(config.getValue()+str);
		boxPkgService.updateBoxRemark(terminal);
		return "success";
	}
	
	/**
	 * 获取终端详情
	 * 
	 * @return
	 */
	public String getTerminalDetails() {
		terminal = terminalDao.getTerminalById(terminalID);
		boxDetails = new BoxDetails(boxDao.boxStatusDetail(terminalID, -1, -1),
				boxDao.boxStatusDetail(terminalID, -1, 1),
				boxDao.boxStatusDetail(terminalID, -1, 0),
				boxDao.boxStatusDetail(terminalID, -1, 5),
				boxDao.boxStatusDetail(terminalID, 1, -1),
				boxDao.boxStatusDetail(terminalID, 1, 1),
				boxDao.boxStatusDetail(terminalID, 2, -1),
				boxDao.boxStatusDetail(terminalID, 2, 1),
				boxDao.boxStatusDetail(terminalID, 3, -1),
				boxDao.boxStatusDetail(terminalID, 3, 1),
				boxDao.boxStatusDetail(terminalID, 4, -1),
				boxDao.boxStatusDetail(terminalID, 4, 1),
				boxDao.boxStatusDetail(terminalID, -1, 2));
		return "details";
	}

	/**
	 * 查看终端密码
	 * 
	 * @return
	 * @throws IOException
	 */
	public String getTermainalPwd() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		terminal = terminalDao.getTerminalById(terminalID);
		out.print(terminal.getTerminalPwd());
		out.close();
		return null;
	}

	/**
	 * 跳转至修改终端密码
	 * 
	 * @return
	 */
	public String linkUpdateTerminalPwd() {
		terminal = terminalDao.getTerminalById(terminalID);
		return "updateTerminalPwd";
	}

	/**
	 * 修改密码
	 * 
	 * @return
	 * @throws IOException
	 */
	public String updateNewPassword() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		try {
			terminal = terminalDao.getTerminalById(terminalID);
			terminal.setTerminalPwd(newpassword);
			terminalDao.edit(terminal);
			out.print("0-"+newpassword);
		} catch (Exception e) {
			out.print("1-"+newpassword);
		} finally {
			out.close();
		}
		return null;
	}

	/**
	 * 验证旧密码
	 * 
	 * @return
	 * @throws IOException
	 */
	public String checkPassword() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if (this.oldpassword.equals(terminalDao.getTerminalById(terminalID)
				.getTerminalPwd() == null ? "" : terminalDao.getTerminalById(
				terminalID).getTerminalPwd())) {
			out.print("0");
		} else {
			out.print("1");
		}
		out.close();
		return null;
	}

	public String edit() {
		System.out.println("terminalID ------->" + terminalID);
		return "success";
	}

	public int getTerminalID() {
		return terminalID;
	}

	public void setTerminalID(int terminalID) {
		this.terminalID = terminalID;
	}

	public List<Terminal> getTerminals() {
		return terminals;
	}

	public void setTerminals(List<Terminal> terminals) {
		this.terminals = terminals;
	}

	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}
	
	public Map<String, Object> getTerminalMap() {
        return terminalMap;
    }

    public void setTerminalMap(Map<String, Object> terminalMap) {
        this.terminalMap = terminalMap;
    }

    public Terminal getTerminal() {
		return terminal;
	}

	public void setTerminal(Terminal terminal) {
		this.terminal = terminal;
	}

	public List<Branch> getBranches() {
		return branches;
	}

	public void setBranches(List<Branch> branches) {
		this.branches = branches;
	}

	public String getOldpassword() {
		return oldpassword;
	}

	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	public Config getConfig() {
		return config;
	}

	public void setConfig(Config config) {
		this.config = config;
	}

	public BoxDetails getBoxDetails() {
		return boxDetails;
	}

	public void setBoxDetails(BoxDetails boxDetails) {
		this.boxDetails = boxDetails;
	}

	public List<Config> getConfigs() {
		return configs;
	}

	public void setConfigs(List<Config> configs) {
		this.configs = configs;
	}

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public String getPageName() {
        return pageName;
    }

    public void setPageName(String pageName) {
        this.pageName = pageName;
    }
    
    public String getIds() {
        return ids;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }
    
    public int getBranchID() {
        return branchID;
    }

    public void setBranchID(int branchID) {
        this.branchID = branchID;
    }

    @Override
    public Terminal getModel() {
        return terminal;
    }
	
}
